﻿using MediatR;
using AutoMapper;
using System.Linq.Expressions;
using Management.API.Application.Controller.Management;
using Management.API.DTOs.Management;
using Management.Domain;
using Management.Errorcode;
using Management.Errorcode.ErrorCode;
using Management.Infrastructure.Interfaces;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Models.BOMBasicInformation;
using Management.API.Utils;
using MySqlX.XDevAPI.Common;
using System.Collections.Immutable;

namespace Management.API.Application.CommandHandler.Management.BasicInformation
{
    public class GetBasicInformationCommandHandler :IRequestHandler<GetBasicInformationCommand,ApiResult<PagingResult<IList<GetBasicInformationDTO>>>>
    {
        private readonly IBasicInformationRepository _iBasicInformationRepository;
        private readonly IMaterialRepository _iMaterialRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetBasicInformationCommandHandler> _logger;

        public GetBasicInformationCommandHandler(IBasicInformationRepository IBasicInformationRepository,
            IMaterialRepository IMaterialRepository, IMapper mapper, ILogger<GetBasicInformationCommandHandler> logger)
        {
            _iBasicInformationRepository = IBasicInformationRepository;
            _iMaterialRepository = IMaterialRepository;
            _mapper = mapper;
            _logger = logger;
        }

   
        async Task<ApiResult<PagingResult<IList<GetBasicInformationDTO>>>> IRequestHandler<GetBasicInformationCommand, ApiResult<PagingResult<IList<GetBasicInformationDTO>>>>.Handle(GetBasicInformationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //
                var BasicInformations = await _iBasicInformationRepository.GetAllAsync();
                var Materials = await _iMaterialRepository.GetAllAsync();

                // 外健链接
                var result = from BasicInformation in BasicInformations
                             join Material in Materials on BasicInformation.MaterialTypeId equals Material.MaterialId
                             select new GetBasicInformationDTO
                             {
                                 BomNumber = BasicInformation.BomNumber,
                                 BomNumberingState = BasicInformation.BomNumberingState,
                                 BomVersionNumber = BasicInformation.BomVersionNumber,
                                 BomSpecificationModel = BasicInformation.BomSpecificationModel,
                                 BomDefault = BasicInformation.BomDefault,
                                 BomDailyOutput = BasicInformation.BomDailyOutput,
                                 BomRemark = BasicInformation.BomRemark,
                                 BomUsageAmount = BasicInformation.BomUsageAmount,
                                 BomRatioMaterialsUsed = BasicInformation.BomRatioMaterialsUsed,
                                 BomNameProcessRoute = BasicInformation.BomNameProcessRoute,
                                 BomProcessNumber = BasicInformation.BomProcessNumber,
                                 BomProcessSpecification = BasicInformation.BomProcessSpecification,
                                 UnitName = BasicInformation.UnitName,
                                 UnitId = BasicInformation.UnitId,
                                 MaterialTypeId = BasicInformation.MaterialTypeId,
                                 ProductTypeId = BasicInformation.ProductTypeId,
                             };

                // 当前页的数据
                var currentData = _mapper.Map<IList<GetBasicInformationDTO>>(result.Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize).ToList());

                // 分页
                var pagingResult = new PagingResult<IList<GetBasicInformationDTO>>
                {
                    TotalCount = result.Count(),
                    TotalPage = Math.Ceiling(result.Count() / (double)request.PageSize),
                    Data = currentData
                };

                // 返回成功结果
                return ApiResult<PagingResult<IList<GetBasicInformationDTO>>>.Success(pagingResult, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "根据 ID 查询BOM失败");
                throw;
            }


        }
    }
}
